<?php

namespace maotool\office\xlsx;

use maotool\core\request\ExportParam;
use maotool\core\request\PageParam;
use maotool\log\Console;
use maotool\object\ObjUtil;
use maotool\util\ArrayUtil;

class Sheet
{

    /**
     * 标题
     * @var
     */
    public $title;

    /**
     * 头部信息
     * @var
     */
    protected $header;

    /**
     * 原始数据
     * @var
     */
    protected $data;

    /**
     * 列表数据
     * @var
     */
    protected $list;

    /**
     * 表格数据
     * @var
     */
    protected $table;

    /**
     * 映射器
     * @var
     */
    protected $mapper;

    public function __construct()
    {
        $this->header = [];
        $this->mapper = [];
        $this->data = [];
        $this->list = [];
        $this->table = [];
    }

    /**
     * @return array
     */
    public function getHeader()
    {
        return $this->header;
    }

    /**
     * @param array $header
     */
    public function setHeader($header)
    {
        $this->header = $header;
    }

    /**
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param array $data
     */
    public function setData($data)
    {
        $this->data = $data;
    }

    /**
     * @param $item
     * @return void
     */
    public function addData($item)
    {
        $this->data[] = $item;
    }

    /**
     * @return array
     */
    public function getTable()
    {
        return $this->table;
    }

    /**
     * @param array $table
     */
    public function setTable($table)
    {
        $this->table = $table;
    }

    /**
     * @param $item
     * @return void
     */
    public function addTable($item)
    {
        $this->table[] = $item;
    }

    /**
     * @return array
     */
    public function getList()
    {
        return $this->list;
    }

    /**
     * @param array $list
     */
    public function setList($list)
    {
        $this->list = $list;
    }

    /**
     * @param $item
     * @return void
     */
    public function addList($item)
    {
        $this->list[] = $item;
    }

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param string $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * 分页获取数据
     * @param $next
     * @param $exportParam ExportParam
     * @return void
     */
    public function pagination($next, $exportParam = null)
    {
        $isHasNextPage = true;
        if (ObjUtil::isNull($exportParam)) {
            $exportParam = new ExportParam();
        }
        $pageNo = $exportParam->getPageNo();
        $pageTotal = 0;
        while ($isHasNextPage) {
            $pageParam = new PageParam();
            $pageParam->setPageNo($pageNo);
            $pageParam->setPageSize($exportParam->getPageSize());
            $pageParam->setParam($exportParam->getParam());
            $exportResult = $next($pageParam);
            $this->setList(ArrayUtil::merge($this->getList(), $exportResult->getList()));
            $isHasNextPage = $exportResult->isHasNextPage();
            $pageTotal = $pageTotal + $exportResult->getSize();
            Console::debug("Xlsx 分页数据加载中 当前是第 " . $pageNo . " 分片 分片数据数量 " . $exportResult->getSize() . " 总数量:" . $pageTotal);
            if ($isHasNextPage) {
                ++$pageNo;
            }
        }
        Console::debug("Xlsx 分页数据加载完成 当前是第 " . $pageNo . " 分片  总数量:" . $pageTotal);
    }

    /**
     * @return array
     */
    public function getMapper()
    {
        return $this->mapper;
    }

    /**
     * @param array $mapper
     */
    public function setMapper($mapper)
    {
        $this->mapper = $mapper;
    }

}